package com.jl.js.console;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.IHyperlink;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

import com.jl.js.filepath.FilePathUtil;
import com.jl.smartcard.core.ActivatorCore;
import com.jl.smartcard.core.CorePluginRuntime;

public class JavaScriptHyberlink implements IHyperlink{

	private TextConsole console;

	public JavaScriptHyberlink(TextConsole console) {
		this.console = console;
	}

	@Override
	public void linkEntered() {
	}

	@Override
	public void linkExited() {
	}

	@Override
	public void linkActivated() {
		String linkText = getLinkText();
		String path = linkText.substring(0,linkText.lastIndexOf(':'));
		int lineNumber = Integer.parseInt(linkText.substring(linkText.lastIndexOf(':') + 1)) - 1;
		String editorId = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(path).getId();
		//IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
		IWorkbenchPage page = ActivatorCore.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
		try {
			FileEditorInput editorInput = new FileEditorInput(getIFile(path));
			IEditorPart editorPart=  page.openEditor(editorInput, editorId);
			ITextEditor textEditor = (ITextEditor)editorPart;
			IDocumentProvider provider = textEditor.getDocumentProvider();
			IDocument document = provider.getDocument(editorInput);
			try {
				provider.connect(editorInput);
				IRegion line = document.getLineInformation(lineNumber);
				textEditor.selectAndReveal(line.getOffset(), line.getLength());
			} catch (CoreException e) {
				e.printStackTrace();
			} catch (BadLocationException e) {
				e.printStackTrace();
			}finally{
				provider.disconnect(editorInput);
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private IFile getIFile(String path) {
		String projectPath = (String) CorePluginRuntime.getValue(CorePluginRuntime.PROJECT_CURRENT_ROOT_NO_SEGMENT);
		projectPath = FilePathUtil.replaceAllAndRemoveEndSegment(projectPath);
		int index = projectPath.lastIndexOf('\\');
		String projectName = projectPath.substring(index);
		IWorkspace ws = ResourcesPlugin.getWorkspace();
		IProject project = ws.getRoot().getProject(projectName);
		index = path.lastIndexOf(projectPath) + projectPath.length() + 1;
		IFile file = project.getFile(path.substring(index));
		return file;
	}

	private String getLinkText(){
		IDocument document = this.console.getDocument();
        IRegion region = this.console.getRegion(this);
        try {
			return document.get(region.getOffset(), region.getLength());
		} catch (BadLocationException e) {
			throw new RuntimeException("Hyperlink Error");
		}
	}

}
